new Vue({
	el: "body",
	data: {
		name: "",
		pwd: "",
		reged: false,
		unpwd: false,
		unpwd1: false,
		loading: true
	},
	methods: {
		log: function() {
			this.reged = false;
			this.unpwd = false;
			this.unpwd1 = false;
			var name = this.name;
			if(!(/^1[34578]\d{9}$/.test(name))) {
				this.unpwd1=true;
			} else {
				var pwd = this.pwd;
				this.login(name, pwd);
			}
		},
		login: function(name, pwd) {
			//登录
			var n ="gryh"+name;
			var p = pwd;
			var _this = this;
			AV.User.logIn(n, p).then(function(loginedUser) {
				console.log(loginedUser);
				history.go(-1); //登录成功
				location.href="../home/home.html";
			}, function(error) {
				console.log(error.code)
				if(error.code == 211) { //未注册
					_this.reg(n, p);
				}
				if(error.code == 210) {
					//密码错误
					_this.unpwd = true;
				}
			});
		},
		reg: function(name, pwd) {
			var _this = this;
			//注册
			// 新建 AVUser 对象实例
			var user = new AV.User();
			// 设置用户名
			user.setUsername(name);
			// 设置密码
			user.setPassword(pwd);
			user.signUp().then(function(loginedUser) {
				console.log("success");
				AV.User.logIn(name, pwd).then(function(loginedUser) {
					console.log(loginedUser);
				}).then(function() {
					var currentUser = AV.User.current();
					var userid = currentUser.id;
					if(currentUser) {
						var userid = currentUser.id;
						var user = AV.Object.createWithoutData('_User', userid);
						console.log(userid);
						/*user.set('isNewUser', true);
						user.set('integral', "0");
						user.set('Money', 0);
						user.save();*/
					}
				}).then(function(){
					alert("新用户注册成功，再次点击可以登录");
				})
			}, function(error) {
				console.log(error);
				//的点点滴滴多多多多多多多多多多多多多多多多多多多多多多多多多多多多多
				if(error.code == 201) { //已注册
					_this.reged = true;
				}
			});
		}
	},
	created: function() {
		init();
	}
})